package com.dkt.android.mvp.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.dkt.android.R
import com.dkt.android.base.BaseFragment
import com.dkt.android.core.RouterHub
import com.dkt.android.mvp.contract.ListContract
import com.dkt.android.mvp.contract.PageContract
import com.dkt.android.mvp.model.IndexShopBean
import com.dkt.android.mvp.presenter.ListPresenter
import com.dkt.android.mvp.presenter.PagerPresenter
import com.dkt.android.mvp.ui.adapter.IndexShopAdapter
import com.dtb.utils.commons.toast.Terror
import com.dtb.utils.commons.toast.Tnormal
import com.dtb.utils.commons.utils.findView
import com.scwang.smartrefresh.layout.SmartRefreshLayout

@Route(path = RouterHub.APP_SHOPPING)
class ShoppingFragment : BaseFragment(), PageContract.View, ListContract.View{

    private var mainAdapter: DelegateAdapter? = null
    private var indexShopAdapter: IndexShopAdapter? = null
    private val shopList = mutableListOf<IndexShopBean>()

    var baseRy: RecyclerView? = null
    var bufferLy: LinearLayout? = null
    var refreshLayout: SmartRefreshLayout? = null
    var key:String =""

    private val pagePresenter:PageContract.Presenter by lazy {
        val b = PagerPresenter()
        b.register(this@ShoppingFragment, handler)
        return@lazy b
    }
    private val listPresenter:ListContract.Presenter by lazy {
        val b = ListPresenter()
        b.register(this@ShoppingFragment, handler)
        return@lazy b
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.public_base_refresh_ry, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        initData()
    }

    private fun initData(){
        pagePresenter.getList(this, false)
    }


    private fun initView(view: View){
        baseRy = view.findViewById(R.id.baseRy)
        bufferLy = view.findViewById(R.id.bufferLy)
        refreshLayout = view.findViewById(R.id.refresh_layout)

        refreshLayout?.setOnLoadMoreListener {
            pagePresenter.getList(this, true)
            it.finishLoadMore(3000)
        }

        refreshLayout?.setOnRefreshListener {
            pagePresenter.getList(this, false)
            it.finishRefresh(3000)
        }

        val layoutManager = VirtualLayoutManager(this@ShoppingFragment.activity!!)
        mainAdapter = DelegateAdapter(layoutManager, false)
        indexShopAdapter= IndexShopAdapter(shopList, LinearLayoutHelper() as LayoutHelper)
        indexShopAdapter?.setOnListener { position, _, _ ->
            activity?.Tnormal("点击第${position+1}个商品 ")
        }
        findView<ImageButton>(R.id.select)?.setOnClickListener {
            key=findView<EditText>(R.id.key)?.text.toString()
            pagePresenter.getList(this, false)
        }

        mainAdapter?.addAdapter(indexShopAdapter)
        baseRy?.layoutManager = layoutManager
        baseRy?.adapter = mainAdapter
    }


    override fun refreshList(isAdd: Boolean, pager: Int) {
        listPresenter.refreshList(this, isAdd, pager.toString(), key)
        if(key.isNotEmpty()){
            key=""
            findView<EditText>(R.id.key)?.setText("")
        }
    }

    override fun refreshView(list: MutableList<IndexShopBean>) {
        if(list.size!=0){
            bufferLy?.visibility=View.GONE
            baseRy?.visibility=View.VISIBLE
            shopList.clear()
            shopList.addAll(list)
            indexShopAdapter?.notifyDataSetChanged()
        }else{
            bufferLy?.visibility=View.VISIBLE
            baseRy?.visibility=View.GONE
            findView<ImageView>(R.id.img)?.setBackgroundResource(R.mipmap.tips_no_content)
            findView<TextView>(R.id.content)?.text="未找到相关商品哦！"
        }
    }

    override fun refreshOnEnd(isAdd: Boolean) {
        refreshLayout?.finishLoadMore()?.finishRefresh()
    }

    override fun back() {
        activity?.Terror("没有更多的内容了")
        pagePresenter.pageBack()
    }

    override fun err() {
        bufferLy?.visibility=View.VISIBLE
        baseRy?.visibility=View.GONE
        findView<ImageView>(R.id.img)?.setBackgroundResource(R.mipmap.tips_no_wifi)
        findView<TextView>(R.id.content)?.text="好像被什么奇怪的东西干扰了？\n没有网络信号噢~"
    }

}